package com.dotflat.staffExtend.controller;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankCircle;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankLog;
import com.dotflat.staffExtend.po.StaffExtendInfoRankLogPO;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankCircleRepository;
import com.dotflat.staffExtend.service.StaffExtendInfoRankLogService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.model.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/staff_rank/log")
@Slf4j
@Api(tags = "员工评分 - 评分记录流水（通过新增流水来变更员工评分）")
public class APIStaffExtendInfoRankLogController extends RestfulBaseController<StaffExtendInfoRankLog, StaffExtendInfoRankLogService, StaffExtendInfoRankLogPO> {

    @Autowired
    private StaffExtendInfoRankCircleRepository circleRepository;

    @Autowired
    public void setBaseService(StaffExtendInfoRankLogService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    @Override
    public StatusResponse add(@RequestHeader(TICKET) String ticket, StaffExtendInfoRankLogPO param){

        StaffDTO staff = cache.getStaff(ticket);

        StaffExtendInfoRankCircle circle = circleRepository.findFirstByAreaId(staff.getAreaId());

        if (circle == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "没有可用的评分周期");
        }

        param.setAreaId(staff.getAreaId());
        param.setSourceType(0);
        param.setSourceId(staff.getId());
        param.setSourceHeadImgUrl(staff.getHeadImgUrl());
        param.setRankCircleId(circle.getId());
        param.setSourceName(staff.getRealName());

        StaffExtendInfoRankLog ret = baseService.add(staff.getAreaId(), staff.getId(), param);

        List<StaffExtendInfoRankLog> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }
}
